package com.power.sifa.correction.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.power.sifa.correction.model.pojo.bo.CheckRuleRequestBO;
import com.power.sifa.correction.model.pojo.bo.CheckRuleResultBO;
import com.power.sifa.correction.model.pojo.bo.rule.*;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;



import java.util.*;

/**
 * @ClassName RuleHandle
 * @Author xiongzw
 * @Date 2023/12/10 11:00
 * @Description 规则处理类
 * @Version: 1.0
 **/

@Component
public class RuleHandleService {

    /**
     * 进行规则校验
     * @param checkRuleRequest
     */
    @Transactional(rollbackFor = Exception.class)
    public List<CheckRuleResultBO> ruleValidate(CheckRuleRequestBO checkRuleRequest) {
        if (ObjectUtil.isNotNull(checkRuleRequest)) {
            AbstractRuleHandler ruleHandler = null;

            if (CollectionUtil.isNotEmpty(checkRuleRequest.getZoneInRules())) {
                ruleHandler = new ZoneInRuleHandler();
            }
            if (CollectionUtil.isNotEmpty(checkRuleRequest.getZoneOutRules())) {
                if (ObjectUtil.isNull(ruleHandler)) {
                    ruleHandler = new ZoneOutRuleHandler();
                } else {
                    ruleHandler.initNextRule(ZoneOutRuleHandler::new);
                }
            }
            if (CollectionUtil.isNotEmpty(checkRuleRequest.getNightOutRules())) {
                if (ObjectUtil.isNull(ruleHandler)) {
                    ruleHandler = new NightOutRuleHandler();
                } else {
                    ruleHandler.initNextRule(NightOutRuleHandler::new);
                }
            }
            // FEAT: 去除同案犯聚集规则，但是保留避免后续撤回
//            if (CollectionUtil.isNotEmpty(checkRuleRequest.getSameCasePersonList())) {
//                if (ObjectUtil.isNull(ruleHandler)) {
//                    ruleHandler = new SameCaseGatherRuleHandler();
//                } else {
//                    ruleHandler.initNextRule(SameCaseGatherRuleHandler::new);
//                }
//            }
            // 进行规则校验
            // 结果集合
            List<CheckRuleResultBO> checkRuleResultList = new ArrayList<>();
            if (ObjectUtil.isNotNull(ruleHandler)) {
                ruleHandler.handleRule(checkRuleRequest, checkRuleResultList);
            }
            return checkRuleResultList;
        }
        return Collections.emptyList();
    }


}
